package com.example.makerspace.utils;

import java.util.Random;

/**
 * @author River
 * @version 1.0
 * @date 2023/9/26 10:25
 * @description:
 */
public class ValidateCodeUtils {
    private static final String CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private  static final String NUMBERS="0123456789";

    public static String generateCode(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(index);
            sb.append(randomChar);
        }

        return sb.toString();
    }
    public static String generateCodeNumber(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(NUMBERS.length());
            char randomChar = NUMBERS.charAt(index);
            sb.append(randomChar);
        }

        return sb.toString();
    }

    /**
     * description: 使用 String.format 格式化数字，实现左侧补 0
     * @param num  需要格式化的数字
     * @param digit 生成字符串长度（保留数字位数）
     * @return String
     * @version v1.0
     * @author w
     * @date 2019年7月19日 下午2:14:31
     */
    public static String fillString(int num, int digit) {
        /**
         * 0：表示前面补0
         * digit：表示保留数字位数
         * d：表示参数为正数类型
         */
        return String.format("%0"+digit+"d", num);
    }

    public static void main(String[] args) {
            String fillString = fillString(1 , 5);
            System.out.println(fillString);
            fillString = fillString(110 , 5);
            System.out.println(fillString);
        }
    }
